package com.xd.cloud.service.vehicle;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xd.cloud.dao.Page;
import com.xd.cloud.dao.device.DeviceDao;
import com.xd.cloud.dao.vehicle.VehicleInfoDao;
import com.xd.cloud.dao.vehicle.VehicleTeamDao;
import com.xd.cloud.entity.device.Device;
import com.xd.cloud.entity.vehicle.VehicleInfo;
import com.xd.cloud.entity.vehicle.VehiclePo;
import com.xd.cloud.entity.vehicle.VehicleTeam;

/**
 * @author wangtao
 * @class_name VehicleService
 * @description Vehicle业务层实现类
 * @create 2017/12/6
 */
@Service
public class VehicleInfoService {
    @Autowired
    private VehicleInfoDao vehicleInfoDao;
    @Autowired
    private DeviceDao deviceDao;
    @Autowired
    private VehicleTeamDao vehicleTeamDao;
    
    public VehicleInfo get(int id)
    {
    	return vehicleInfoDao.get(id);
    }
    
    public Page getAll(int pageNo, int pageSize) {
    	return vehicleInfoDao.getAll(pageNo, pageSize);
    }
    
    public boolean remove(int id)
	{
    	VehicleInfo c = vehicleInfoDao.get(id);
		if (c != null)
		{
			vehicleInfoDao.delete(c);
			return true;
		}
		else
		{
			return false;
		}
	}
    
    public boolean saveVehicle(VehicleInfo info) {
		
    	VehicleInfo u = vehicleInfoDao.getByVehid(info.getVehid());
		if (u != null)
		{
			return false;
		}
		vehicleInfoDao.save(info);
		return true;
	}
    
    public boolean update(VehicleInfo info) {		
    	vehicleInfoDao.update(info);		
		return true;
	}
    

    public boolean updateVehicle(int id,String devicecode) {
		
    	VehicleInfo u = vehicleInfoDao.get(id);
		if (u != null)
		{
			u.setDevicecode(devicecode);
			vehicleInfoDao.update(u);
			
			Device device = deviceDao.getDeviceBydeviceid(devicecode);
			device.setInstall(1);
			device.setInstall_time(new Date());
			deviceDao.update(device);
			return true;
		}
		return false;
	}
    
    //所属公司但未分配任何车队的列表
	public List<VehiclePo> getVehicleByCompanyIdNoTeam(int company_id) {
		List<Object[]> list = vehicleInfoDao.getVehicleByCompanyIdNoTeam(company_id);
		List<VehiclePo> retuList = new ArrayList<VehiclePo>();
		if(list!=null && list.size()>0) {
			for (Object[] item:list){
				VehiclePo temp = new VehiclePo();
				temp.setId((Integer)item[0]);
				temp.setVehid((String)item[1]);
				retuList.add(temp);
			}
		}
		return retuList;
	}
	
	//已分配车队的车辆列表
	public List<VehiclePo> getVehicleByTeamId(int team_id) {
		List<Object[]> list = vehicleInfoDao.getVehicleByTeamId(team_id);
		List<VehiclePo> retuList = new ArrayList<VehiclePo>();
		if(list!=null && list.size()>0) {
			for (Object[] item:list){
				VehiclePo temp = new VehiclePo();
				temp.setId((Integer)item[0]);
				temp.setVehid((String)item[1]);
				retuList.add(temp);
			}
		}
		return retuList;
	}
	
	//移至车队
	public boolean removeInTeam(int team_id, int vehicle_id) {
		try {
			VehicleTeam item = new VehicleTeam();
			item.setTeam_id(team_id);
			item.setVehicle_id(vehicle_id);
			vehicleTeamDao.save(item);
			return true;
		}catch(Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	
	//移出车队
	public boolean removeOutTeam(int team_id, int vehicle_id) {
		try {
			VehicleTeam item = vehicleTeamDao.getObject(team_id, vehicle_id);
			if(item!=null) {
				vehicleTeamDao.delete(item);
				return true;
			}
		}catch(Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	public List<VehicleInfo> getAllVehicle() {
		return vehicleInfoDao.getAllVehicle();
	}
}
